package mission;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import ucar.ma2.Array;
import ucar.nc2.NetcdfFile;
import utils.ReadAllNcDataUtils;
import utils.saveAsExcelUtils;

import java.util.List;

public class nc2Excel {
    //存放掩星数据的文件夹
    private static String yourFilePath = "D:\\掩星数据\\2019年\\month1";

    public static void main(String[] args) {
        //将获得的数据保存为excel
        saveToExcel();
    }

    private static void saveToExcel() {
        ReadAllNcDataUtils ro = new ReadAllNcDataUtils();
        List<NetcdfFile> nc = ro.getNcDataList(yourFilePath);

        //获得海拔数据
        List<Array> altList = ro.getAltList(nc);
        //将掩星点经度数据保存到Excel
        saveAsExcelUtils se = new saveAsExcelUtils();
        if (altList.size() != 0) {
            makeData(altList.get(0));
        }

    }

    private static void makeData(Array altList) {
        //第一步，创建一个workbook对应一个excel文件
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //第二部，在workbook中创建一个sheet对应excel中的sheet
        HSSFSheet sheet = hssfWorkbook.createSheet("用户表一");
        //第三部，在sheet表中添加表头第0行，老版本的poi对sheet的行列有限制
        HSSFRow row = sheet.createRow(0);
        //第四步，创建单元格，设置表头
        HSSFCell cell;
        cell = row.createCell(0);
        cell.setCellValue("alt");

        for (int i = 0; i < altList.getSize(); i++) {
            HSSFRow row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(altList.getDouble(i));
        }

        saveAsExcelUtils.saveAsExcel("你要存储的位置","/GlobalVariables.xls",hssfWorkbook);
    }
}
